<template>
    <div>
        <mu-appbar style="width: 100%;" color="primary">
        益多多
        </mu-appbar>
         <h2 style="color:#757575;"> 登录 </h2>
        <mu-container>
        <mu-text-field v-model="username" label="请输入账户名" label-float  icon="account_circle"></mu-text-field><br/>
        <mu-text-field v-model="password" type="password" label="请输入密码" label-float  icon="locked"></mu-text-field><br/>
        </mu-container>

        <mu-container class="button-wrapper">
        <mu-button color="primary" @click="login()">登录</mu-button>
        <mu-button color="success" @click="goRegister()">注册</mu-button> 
        <br>
        <mu-button flat to="/FoundPsw"> 找回密码 </mu-button>
        </mu-container>
    </div>
</template>
<script>
import axios from "axios";
import Toast from "muse-ui-toast";
export default {
    data(){
return{
    username:"",
    password:"",
      _name:'',
      _role:''
}

    },
    mounted(){
         axios.get('/api/user/layin')
        .then(result=> {
            if(result.data.length !== 0 ){
                if(result.data.role === 10){
                    this._role = "个人"
                    this.$router.push('/Person')
                }else if(result.data.role === 20){
                    this._role = "组织"
                    this.$router.push('/Origanization')
                }else if(result.data.role === 30){
                    this._role = "管理员"
                    this.$router.push('/adminLogin')
                }
                
                this._name = result.data.name;
                console.log("登录状态");
                
            }else{
                this._name = '';
                console.log("注销状态");
            }
        })
        .catch(function (err) {
            console.log(err);
        }); 
    },
    methods:{
        login(){ 
            if(this.username=="" || this.password==""){
                console.log('请输入用户名/密码')
                Toast.warning('请输入用户名/密码');
            }      else{  
            let data={username:this.username,password:this.password};
            axios.post('/api/user/login',{data:data}).then(result=>{
                        if(result.data=='登录成功'){
                         console.log('登录成功')
                         Toast.success('登录成功');
                          setTimeout(() => {this.$router.push('/Person') }, 1500);
                     }else if(result.data=='请进行组织认证'){
                         console.log('请进行组织认证')
                         Toast.success('登录成功 请进行组织认证');
                         setTimeout(() => {this.$router.push('/Origanization') }, 1500);
                     }else if(result.data=='认证组织登录成功'){
                         console.log('认证组织登录成功')
                         Toast.success('登录成功');
                         setTimeout(() => {this.$router.push('/Origanization') }, 1500);
                     }else if(result.data=='管理员登录成功'){
                         console.log('管理员登录成功')
                         Toast.success('登录成功');
                         setTimeout(() => {this.$router.push('/adminLogin') }, 1500);
                     }      
                     else if(result.data=="用户不存在"){
                       Toast.error('用户不存在');
                         console.log('用户不存在')
                     }else if(result.data=='登录失败'){
                         console.log('登录失败')
                         Toast.error('登录失败');
                     }
            })}
        },
        goRegister(){
            this.$router.push('/register')
}
 
    }
}
</script>
<style lang="less">
.button-wrapper {
  text-align: center;
  .mu-button {
    margin: 8px;
  }
}
</style>